<?php
// +----------------------------------------------------------------------
// | [RhaPHP System] Copyright (c) 2017 http://www.rhaphp.com/
// +----------------------------------------------------------------------
// | [RhaPHP] 并不是自由软件,你可免费使用,未经许可不能去掉RhaPHP相关版权
// +----------------------------------------------------------------------
// | Author: Geeson <qimengkeji@vip.qq.com>
// +----------------------------------------------------------------------

namespace app\mp\controller;
use think\Db;
use think\Request;
use app\admin\controller\Base;
use think\Validate;

class Index extends Base
{
    public function _initialize()
    {
        return parent::_initialize(); // TODO: Change the autogenerated stub
    }

    public function index($mid)
    {
        if(empty($mid) && !is_numeric($mid)){
            $this->error('参数有误');
        }
        $Mp=Db::name('mp')->where(['id'=>$mid])->find();
        $data['url']= getHostDomain().url('mp/Entr/index',['mid'=>$mid]);
        $data['valid_token']=$Mp['valid_token'];
        $data['encodingaeskey']=$Mp['encodingaeskey'];
        $this->assign('mp',$data);
       return view();

    }

    public function MpList(){
        $MpList=Db::name('mp')
            ->where(['user_id'=>$this->admin_id])
            ->select();
        foreach ($MpList as $key=>$v){
            $MpList[$key]['type']=getMpType($v['type']);
        }
        $this->assign('menu_title','公众号管理');
        $this->assign('mpList',$MpList);
        return view();
    }

    public function addMp(){

        if(Request::instance()->isAjax()){
            $data = input('post.');
            unset($data['image']);
            $validate = new Validate(
                [
                    'name' => 'require',
                    'appid' => 'require',
                    'appsecret' => 'require',
                    'logo' => 'require',
                    'qrcode' => 'require',
                ],
                [
                    'name.require' => '公众号名称不能为空',
                    'appid.require' => ' Appid不能为空',
                    'appsecret.require' => 'Appsecret不能为空',
                    'logo.require' => 'Logo不能为空',
                    'qrcode.require' => '二维码不能为空',
                ]
            );
            $result = $validate->check($data);
            if ($result === false) {
                ajaxMsg(0, $validate->getError());
            }
            $data['create_time']=time();
            $data['user_id']=$this->admin_id;
            $data['valid_token']=getRandChar('32');
            $data['token']=getRandChar('32');
            $data['encodingaeskey']=getRandChar('43');
            $mid = Db::name('mp')->insertGetId($data);
            if($mid){
                ajaxMsg(1,'操作成功');
            }else{
                ajaxMsg(0,'操作失败');
            }
        }
        return view();
    }

    public function updateMp($id=''){
        $mode = new \app\common\model\Mp();
        if(Request::instance()->isAjax()){
            $data=input('post.');
            $mode->allowField(true)->save($data,['id'=>$data['id'],'user_id'=>$this->admin_id]);
                ajaxMsg(1,'更改成功');
        }else{
            if(!$mp=$mode->where(['id'=>$id,'user_id'=>$this->admin_id])->find()){
                $this->error('没有此公众号');
            }
            $this->assign('mp',$mp);
            $this->assign('menu_time','修改公众号');
            return view();
        }
    }

    public function delMp($id){
        $mode = new \app\common\model\Mp();
        $mode->where(['id'=>$id,'user_id'=>$this->admin_id])->delete();
        ajaxMsg(1,'操作成功');
    }
}


